package com.youbug.custservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.custservice.entity.*;
import com.youbug.custservice.service.*;
import com.youbug.custservice.vo.ChatVo;
import com.youbug.common.vo.R;
import com.youbug.system.entity.Customer;
import com.youbug.custservice.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 会话表 前端控制器
 * </p>
 *
 * @author youbug
 * @since 2023-01-07
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private IChatService chatService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private IServiceTicketService serviceTicketService;

    @Autowired
    private ICustomerService customerService;

    @PostMapping("/getChats")
    public R<IPage<Chat>> getChats(@RequestBody ChatVo chatVo){

        IPage<Chat> chats = chatService.getChatPage(chatVo);

        return R.ok(chats);
    }

    @GetMapping("/getChatMessages/{chatId}")
    public R<List<Message>> getChatMessages(@PathVariable Long chatId){

        QueryWrapper<Message> wrapper = new QueryWrapper <> ();

        wrapper.eq("chat_id", chatId);
        wrapper.orderByAsc("create_time");
        List<Message> messages = messageService.list(wrapper);

        return R.ok(messages);
    }

    @GetMapping("/getTicket/{chatId}")
    public R<ServiceTicket> getTicket(@PathVariable Long chatId){

        QueryWrapper<ServiceTicket> wrapper = new QueryWrapper <> ();

        wrapper.eq("chat_id", chatId);

        ServiceTicket ticket = serviceTicketService.getOne(wrapper);

        return R.ok(ticket);
    }

    @GetMapping("/getClientInfo/{clientId}")
    public R<Customer> getChatInfo(@PathVariable Integer clientId){

        QueryWrapper<Customer> wrapper = new QueryWrapper <> ();

        wrapper.eq("id", clientId);

        Customer customer = customerService.getOne(wrapper);

        return R.ok(customer);
    }


    @PostMapping("/download")
    public R<String> download(@RequestBody ChatVo chatVo){

        String fileUrl = chatService.download(chatVo);

        return R.ok(fileUrl);
    }
}

